package com.foreknow.controller;

import com.foreknow.bean.Guest;
import com.foreknow.service.GuestService;
import com.foreknow.service.impl.GuestServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class SelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型：text/html
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端请求的编码格式(否则会产生乱码)  默认：iOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        GuestService service = new GuestServiceImpl();
        List<Object> list = service.Query() ;
        //通过request.setAttribute("list",list);在服务器端保存list集合
        req.setAttribute("list",list);
        //转发资源到list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
//        out.println("<html>");
//        out.println("<head><title>select servlet</title></head>");
//        out.println("<body>");
//        out.println("<table border=\"1px soild \">");
//        out.println("<tr>");
//        out.println("<th>id</th>");
//        out.println("<th>name</th>");
//        out.println("<th>phone</th>");
//        out.println("<th>title</th>");
//        out.println("<th>email</th>");
//        out.println("<th>context</th>");
//        out.println("<th>createdtime</th>");
//        out.println("</tr>");
//        for (int i = 0 ;i < list.size();i++){
//            Guest guest = (Guest) list.get(i);
//            out.println("<tr>");
//            out.println("<td>"+guest.getId()+"</td>");
//            out.println("<td>"+guest.getName()+"</td>");
//            out.println("<td>"+guest.getPhone()+"</td>");
//            out.println("<td>"+guest.getTitle()+"</td>");
//            out.println("<td>"+guest.getEmail()+"</td>");
//            out.println("<td>"+guest.getContent()+"</td>");
//            out.println("<td>"+guest.getCreatedtime()+"</td>");
//            out.println("</tr>");
//        }
//        out.println("</table>");
//        out.println("</body>");
//        out.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
